package com.test.service;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.tree.DefaultDocument;

import com.test.dao.OrderDetailDAO;
import com.test.entity.OrderDetail;
import com.test.entity.UserInfo;

public class OrderDetailService {
	private OrderDetailDAO orderDetailDAO;

	public void saveOrdersdetail(OrderDetail orderDetail) {
		orderDetailDAO.saveOrdersdetail(orderDetail);
	}

	public OrderDetailDAO getOrderDetailDAO() {
		return orderDetailDAO;
	}

	public void setOrderDetailDAO(OrderDetailDAO orderDetailDAO) {
		this.orderDetailDAO = orderDetailDAO;
	}

	public String queryOrderDetailXmlByOrderId(String orderId, UserInfo user) {
		System.out.println(orderId);
		List<OrderDetail> list = orderDetailDAO.queryDetails(orderId);

		Document document = new DefaultDocument();
		Element infoElement = document.addElement("info");

		for (OrderDetail od : list) {
			Element odElement = infoElement.addElement("orderdetail");
			Element nameElement = odElement.addElement("name");
			nameElement.setText(od.getCardType().getCardTypeName());
			Element priceElement = odElement.addElement("price");
			priceElement.setText("" + od.getCardType().getCardPrice());
			Element quantityElement = odElement.addElement("quantity");
			quantityElement.setText("" + od.getQuantity());
		}

		Element moneyElement = infoElement.addElement("money");
		moneyElement.setText("" + user.getMoney());
		System.out.println(document.asXML());
		return document.asXML();
	}

}
